import requests


# url = "https://pvp.qq.com/web201605/js/herolist.json"
#
# res = requests.get(url)
# for data in res.json():
#     print(data)
#     cname = data['cname']
#     head_url = f'https://game.gtimg.cn/images/yxzj/img201606/heroimg/{data["ename"]}/{data["ename"]}.jpg'
#     head_res = requests.get(url=head_url)
#     with open(f'{cname}.jpg', 'wb') as f:
#         f.write(head_res.content)
#     break


class WZSpider:
    def __request(self, url, parser, **kwargs):
        self.res = requests.get(url)
        parser(self.res, **kwargs)

    def __parser_head(self, res, **kwargs):
        cname = kwargs.get("cname")
        with open(f'wzhead/{cname}.jpg', 'wb') as f:
            f.write(res.content)

    def __parser(self, res):
        for data in res.json():
            print(data)
            cname = data["cname"]
            head_url = f'https://game.gtimg.cn/images/yxzj/img201606/heroimg/{data["ename"]}/{data["ename"]}.jpg'
            self.__request(head_url, self.__parser_head, cname=cname)


    def start(self, url):
        self.__request(url, self.__parser)

wz = WZSpider()
wz.start("https://pvp.qq.com/web201605/js/herolist.json")